import reflectiondemo.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) {

    }
    public static void main7(String[] args) {
        try{
            Class<?> c1 =  Class.forName("reflectiondemo.Student");

            Method method = c1.getDeclaredMethod("function", String.class);
            method.setAccessible(true);

            Student student = (Student)c1.newInstance();

            method.invoke(student,"这是一个参数....");

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main6(String[] args) {
        try{
            Class<?> c1 =  Class.forName("reflectiondemo.Student");

            Field field = c1.getDeclaredField("name");

            field.setAccessible(true);//同意修改

            Student student = (Student)c1.newInstance();//创建对象

            field.set(student,"lisi");

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main5(String[] args) {
        try{
            Class<?> c1 =  Class.forName("reflectiondemo.Student");

            Constructor<Student> constructor =
                    (Constructor<Student>) c1.getDeclaredConstructor(String.class,int.class);

            constructor.setAccessible(true);

            Student student = (Student) constructor.newInstance("lisi",22);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main4(String[] args) {
        try{
            Class<?> c1 =  Class.forName("reflectiondemo.Student");

            Student student =  (Student)c1.newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main3(String[] args) {
        Class c1 = Student.class;
    }
    public static void main2(String[] args) {
        Student student = new Student();
        Class<?> c1 = student.getClass();
    }
    public static void main1(String[] args) {
        try{
            Class<?> c1 =  Class.forName("reflectiondemo.Student");

            Class<Student> c2 =  (Class<Student>)Class.forName("reflectiondemo.Student");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
